package com.esbi.projectwebsocket.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义拦截器类
 */
public class CustomInterceptor implements HandlerInterceptor {


    // 在请求处理之前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("请求处理之前：" + request.getRequestURI());
        if(request.getRequestURI().startsWith("/test")){
            System.out.println("test拦截他");
            return false;
        }
        // 如果返回 false，则请求被截断，后续的处理器和拦截器不会被执行
        return true;
    }

    // 在请求处理之后，视图渲染之前执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("请求处理之后，视图渲染之前：" + request.getRequestURI());
    }

    // 在整个请求处理完成后执行，无论请求是否成功
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("请求处理完成后：" + request.getRequestURI());
    }
}